package sg.edu.nus.iss.vmcs.store;

import java.util.*;


/**
 * Represents the store of cash in the vending machine. 
 * @version 1.0  07/12/2011
 * @author Team 7S - SE19
 */


public class CashStore extends Store {

 
	public final static int INVALID_COIN_WEIGHT = 9999;
        
/**
 * This constructor creates a new instance of the Cash Store.  
 * It creates the required number of instances of Cash Store Item and Coin objects
 * 
 */
        
	public CashStore() {
	}
        
/**
 * This operation is used to find the index to a Cash Store Item for a specific 
 * Coin object.
 * @param c  The current Coin object.
 * @return   returns an integer of the index.  If no Coin is found, returns -1.
 */
	public int findCashStoreIndex (Coin c) {
		int size = getStoreSize();
		for (int i = 0; i < size; i++) {
			StoreItem item = (CashStoreItem) getStoreItem(i);
			Coin current = (Coin) item.getContent();
			if (current.getWeight() == c.getWeight())
				return i;
		}
		return -1;
	}

        /**
         * This operations returns the Coin object when given the weight of the
         * Coin.
         * @param weight the weight in grams of the coin
         * @return returns the Coin object.
         */
	public Coin findCoin(double weight) {
		double en;
		Coin coin;
		StoreObject so;
		int i;

		for (i = 0; i < size; i++) {
			if (items[i] == null)
				return null;
			so = items[i].getContent();
			if (so == null)
				return null;
			coin = (Coin)so;
			en = coin.getWeight();
			if (en == weight)
				return coin;
		}
		return null;
	}
	
}

